package com.kmmlearn.findtime.android.ui.components

import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.exponentialDecay
import androidx.compose.animation.core.spring
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.gestures.AnchoredDraggableState
import androidx.compose.foundation.gestures.DraggableAnchors
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.anchoredDraggable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt

enum class CardAnchor {
    Collapsed,
    Expanded
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun DraggableCardM3() {
    val anchors = DraggableAnchors<CardAnchor> {
        CardAnchor.Collapsed at 0f
        CardAnchor.Expanded at 500f
    }

    val state = remember {
        AnchoredDraggableState(
            initialValue = CardAnchor.Collapsed,
            anchors = anchors,
            positionalThreshold = { distance: Float -> distance * 0.5f },
            velocityThreshold = { 100f },
            snapAnimationSpec = spring(Spring.DampingRatioMediumBouncy),
            decayAnimationSpec = exponentialDecay()
        )
    }

    Box(
        modifier = Modifier.offset{
            IntOffset(x = state.requireOffset().roundToInt(), y = 0)
        }.anchoredDraggable(state, Orientation.Horizontal)
    ) {
        Box(
            Modifier
                .fillMaxSize()
                .padding(16.dp)
        ) {
            Text(
                text = "Drag me up / down",
                color = MaterialTheme.colorScheme.onPrimaryContainer
            )
        }
    }
}

